<?php
/**
 *	@name		商品拍卖
 *	@author		Jeffery
 *	@qq			1017756891
 *	@email		1017756891@qq.com
 *	@website	http://www.iweitao.net
**/
require_once "core.php";
require_once "paipai.class.php";
//error_reporting(E_ALL);
$core = new safe_weitao_plugin_iweitao_paipai;
$globalclass = new threadplugin_iweitao_paipai;
if($core->_get('act') == 'buy') {
	//获取商品详细信息
	$gid = $core->_get('gid');
	$price = $core->_get('price');
	$tid = $core->_get('tid');
	if(!$globalclass->check_goods_status($tid,'code')) {
		exit("-7");
	} 	
	$array = $globalclass->getgoodsdetail($gid);
	$time = time();
	if($array['uid'] == $_G['uid']) {
		exit("-6");
	}
	if($array['data_start'] > $time) {
		exit("-2");
	}
	if($array['data_end'] < $time) {
		exit("-2");
	}
	if($price < $array['price']) {
		exit("-3");
	}
	//判断交易价格是否合法
	$add_price = $price - $array['price'];
	if($add_price < $array['add_price']) {
		exit("-4");
	}
	
	if($array['identify'] == 'default') {
		$query = DB::query("SELECT * FROM `".DB::table('paipai_buy')."` WHERE `uid` = '".$_G['uid']."'");
		if(mysql_fetch_array($query)) { //判断是否为第一次出价
			$query = DB::query("SELECT max(time) FROM `".DB::table('paipai_buy')."` WHERE `uid` = '".$_G['uid']."'");
			$exists = mysql_fetch_array($query);
			$exists = $exists[0];
			$query = DB::query("SELECT * FROM `".DB::table('paipai_buy')."` WHERE `time` = '$exists'");
			$infos = mysql_fetch_array($query);
			$alreadypay = $infos['price'];
			$needprice = $price-$alreadypay;
		} else {
			$needprice = $price;
		}
		
		if(!checkcredit($needprice)) {
			exit("-5");
		}
		updatemembercount($_G['uid'],array("extcredits$credit" => "-$needprice"));

	} else { //调用扩展模块数据
		$FILE = $array['identify'];
		require_once "./source/plugin/iweitao_paipai/$FILE/ajax_buy.php";
	}
	
	$loadingfile = $globalclass->getapihook('ajax');
	foreach($loadingfile as $k => $v) {
		include_once $v;
	}
	if($updatearray == null) {
		$updatearray = array(
			'price' => $price,
		);
	}
	//更新当前商品出售价
	DB::update("paipai_goods",$array,"`gid` = '$gid'");
	DB::query("UPDATE `".DB::table("paipai_buy")."` SET `status` = '2' WHERE `gid` = '$gid'");
	//插入数据库，修改商品价格
	DB::query("INSERT INTO ".DB::table("paipai_buy")." VALUES(null,'".$_G['uid']."','$gid','$tid','".$_G['username']."','$price','$time','1')");
	notification_add($_G['uid'],'post',"拍卖成功，本次交易金额为".$price,array(),$system = 0);
	exit("1");
	//返回操作结果
} else if($core->_get('act') == 'checkout') {
	$gid = $core->_get('gid');
	$tid = $core->_get('tid');
	if(!$globalclass->check_goods_status($tid,'code')) {
		exit("对不起，当前交易冻结中");
	}
	$array = $globalclass->getgoodsdetail($gid);
	$time = time();
	if($array['status'] == 3) {
		exit("该订单已结算");
	}
	if($_G['uid'] != $array['uid']) {
		exit("您无权进行该操作");
	}
	if($array['data_end'] > $time) {
		exit("对不起，交易尚未结束");
	}
	//返回扣除冻结积分，排除购买成功
	$orderdetail = $globalclass->getorderdetail($tid);
	foreach ($orderdetail as $k => $v) {
		if($v['status'] == 1) {
			$noncount = $v['uid'];
			DB::query("UPDATE `".DB::table("paipai_buy")."` SET `status` = '3' WHERE `orderid` = '".$v['orderid']."'");
			$globalprice = $v['price']; //获取出价最高的积分
			$globaluid = $v['uid'];
			DB::query("UPDATE `".DB::table("paipai_goods")."` SET `status` = '3' WHERE `gid` = '$gid'");
		}
	}
	foreach ($orderdetail as $k => $v) {
		if(!in_array($v['uid'],$uids)) {
			if($v['status'] == 1) {
				continue;
			} else {
				if($v['uid'] == $noncount) {
					continue;
				}
				$uids[] = $v['uid'];
			}
		}
	}
	foreach($uids as $k => $v) {
		$query = DB::query("SELECT max(price) FROM `".DB::table('paipai_buy')."` WHERE `tid` = '$tid' AND `uid` = '$v'");
		$fetch = mysql_fetch_array($query);
		if($array['identify'] == 'default') {
			updatemembercount($v['uid'],array("extcredits$credit" => "$price"));
		} else {
			$FILE = $array['identify'];
			require_once "./source/plugin/iweitao_paipai/$FILE/ajax_checkout.php";
		}
	}
	//修改商品状态
	//返回正常状态
	notification_add($_G['uid'],'post',"<a href='forum.php?mod=viewthread&tid=$tid'>【交易成功您尽快发货】</a>",array(),$system = 1);
	notification_add($globaluid,'post',"<a href='forum.php?mod=viewthread&tid=$tid'>【恭喜您已成功拍的此商品】</a>",array(),$system = 1);
	exit("结算成功本次收入".$globalprice);
} else if($core->_get('act') == 'pay') { 
	$orderid = $core->_get('orderid');
	$order = $globalclass->getorderdetail_by_orderid($orderid);
	$globalclass->check_goods_status($orderid['tid'],'message');
	$tid = $order['tid'];
	if($order['uid'] != $_G['uid']) {
		showmessage("您没有权限进行该操作");
	}
	$array = $globalclass->getgoodsdetail($order['gid']);
	if($order['status'] == 4) {
		showmessage("款项支付成功!",'forum.php?mod=viewthread&tid='.$array['tid']);
	}
	if($array['identify'] == 'default') {
		updatemembercount($array['uid'],array("extcredits$credit" => $order['price']));
		notification_add($array['uid'],'post',"<a href='forum.php?mod=viewthread&tid=$tid'>【买家已付款交易结束】</a>",array(),$system = 1);
	} else {
		$FILE = $array['identify'];
		require_once "./source/plugin/iweitao_paipai/$FILE/ajax_pay.php";
	}
	DB::update("paipai_buy",array('status' => 4),array('orderid' => $orderid));

	showmessage("款项支付成功!",'forum.php?mod=viewthread&tid='.$array['tid']);
	
	//增加卖家积分
	
} else if($core->_get('act') == 'return') { 
	$orderid = $core->_get('orderid');
	$order = $globalclass->getorderdetail_by_orderid($orderid);
	$globalclass->check_goods_status($orderid['tid'],'message');
	$tid = $order['tid'];
	if($order['uid'] != $_G['uid']) {
		showmessage("您没有权限进行该操作");
	}
	if($order['status'] != 3) {
		showmessage("无法请求退款申请,交易状态错误");
	}
	DB::update('paipai_goods',array('freeze'=>1),"tid = $tid");
	notification_add(1,'post',"<a href='forum.php?mod=viewthread&tid=$tid'>【您有一笔新的退款申请】</a>",array(),$system = 1);
	showmessage("退款已申请至管理员，请耐心等待",'forum.php?mod=viewthread&tid='.$tid);
} else if($core->_get('act') == 'tenpay') {
	$orderid = $core->_get('orderid');
	$order = $globalclass->getorderdetail_by_orderid($orderid);
	$globalclass->check_goods_status($orderid['tid'],'message');
	//更新订单状态，并且更新买家积分
	$array = $globalclass->getgoodsdetail($order['gid']);
	$tid = $array['tid'];
	updatemembercount($array['uid'],array("extcredits$credit" => $order['price']));
	DB::update("paipai_buy",array('status' => 5),array('orderid' => $orderid));
	notification_add($array['uid'],'post',"<a href='forum.php?mod=viewthread&tid=".$tid."'>【买家已付款交易结束】</a>",array(),$system = 1);
	showmessage("款项支付成功!",'forum.php?mod=viewthread&tid='.$tid);
}
?>